$(function(){
	//提示
	$('input[type="text"][readonly!="readonly"][name!="obj_date"]').bind('focus',function(){
		if(nullBooleanNumber($(this).val())){
			$(this).val('');
		}
	}).bind('blur',function(){
		if(nullBooleanNumber($(this).val())){
			$(this).val('0.0000');
		}
	});
	//利润实现项目（万元）占营业利润比（%）实现
	$('input[type="text"][readonly!="readonly"][name!="obj_date"]').bind('blur',function(){
		if(nullBooleanNumber($(this).val())){
			$(this).val('0.0000');
		}
		checkLrBl($(this));
	});
	//5.财务利息净收入
	$('#4').bind('blur',function(){
		var thisval = $(this).val();
		thisval = (Nn(thisval)==0?'0.0000':thisval*0.01);
		$('#5').val(Number(thisval).toFixed(4));
		checkLrBl();
	});
	//11.收入计划合计
	$('#4,#7,#8,#9,#10').bind('blur',function(){
			var n_11 = NnSum('#5,#7,#8,#9,#10');
			$('#11').val(Number(n_11==0?'0.0000':n_11).toFixed(4));
			var n_49 = Nn($('#11').val())-Nn($('#48').val());
			$('#49').val(Number(n_49==0?'0.0000':n_49).toFixed(4));
			checkLrBl();
	});
	//16.人工费用合计
	//18.工会经费	 19职工教育经费	  都指向 16  所以一起计算
	$('#13,#14,#15').bind('blur',function(){
			var num_16 = NnSum('#13,#14,#15');
			$('#16').val(Number(num_16).toFixed(4));
			num_16 = num_16*0.02;
			$('#18').val(Number(num_16==0?'0.0000':num_16).toFixed(4));
			num_16 = num_16*0.025;
			$('#19').val(Number(num_16==0?'0.0000':num_16).toFixed(4));
			//管理费用计划小计
			var num_16 = NnSum(num_45);//#18,#19,
			num_16 = (num_16==0?'0.0000':num_16);
			$('#45').val(Number(num_16).toFixed(4));
			//可控费用计划合计
			var n50 = NnSum(num_50);
			$('#50').val(Number(n50==0?'0.0000':n50).toFixed(4));
			checkLrBl();
	});
	//28.办公费合计
	$('#25,#26,#27').bind('blur',function(){
			var num_28 = NnSum('#25,#26,#27');
			$('#28').val(Number(num_28==0?'0.0000':num_28).toFixed(4));
			//可控费用计划合计
			var n50 = NnSum(num_50);
			$('#50').val(Number(n50==0?'0.0000':n50).toFixed(4));
			checkLrBl();
	});
	//34.物业水电费合计
	$('#32,#33').bind('blur',function(){
			var num_34 = NnSum('#32,#33');
			$('#34').val(Number(num_34==0?'0.0000':num_34).toFixed(4));
			checkLrBl();
	});
	//42.住房公积金及租房补合计
	$('#40,#41').bind('blur',function(){
			var num_42 = NnSum('#40,#41');
			$('#42').val(Number(num_42==0?'0.0000':num_42).toFixed(4));
			checkLrBl();
	});
	//45管理费用计划小计 
	var num_45 = "#13,#14,#15,#17,#20,#21,#22,#23,#24,#25,#26,#27,#29,#30,#31,#32,#33,#35,#36,#37,#38,#39,#40,#41,#43,#44";
	$(num_45).bind('blur',function(){
		var n45 = NnSum(num_45);
		n45 = (n45==0?'0.0000':n45);
		$('#45').val(Number(n45).toFixed(4));
		checkLrBl();
	});
	//46主营业务税金及附加 7（计划金额）*5.6%
	//47客户风险准备金       7*5%
	//48营业费用计划合计
	var num_46_47 = "#7";
	$(num_46_47).bind('blur',function(){
		var n46_47 = NnSum(num_46_47);
		var n46 = (n46_47*0.056==0?'0.0000':n46_47*0.056);
		$('#46').val(Number(n46).toFixed(4));
		var n47 = (n46_47*0.05==0?'0.0000':n46_47*0.05);
		$('#47').val(Number(n47).toFixed(4));
		//48营业费用计划合计 46+47
		$('#48').val(Number(NnSum('#47,#46')).toFixed(4));
		checkLrBl();

		//49营业利润计划合计
		var n_49 = Nn($('#11').val())-Nn($('#48').val());
		$('#49').val(Number(n_49==0?'0.0000':n_49).toFixed(4));
		checkLrBl();
		
	});
	var num_50 = "#16,#17,#22,#23,#24,#28,#29,#30";
	$("#17,#22,#23,#24,#29,#30").bind('blur',function(){
		var n50 = NnSum(num_50);
		$('#50').val(Number(n50==0?'0.0000':n50).toFixed(4));
		checkLrBl();
	});
	//50可控费用计划合计
	//16aa（计划金额1）+17（计划金额）+22（计划金额）+23（计划金额）+24（计划金额）+28aa（计划金额）+29（计划金额）+30（计划金额）
	function GetBl(){
		var id,thisval,bl,lr,val_49=$('#49').val(),val_48=$('#48').val(),val_7=$('#7').val();
		$('#3,#4,#5,#6,#7,#8,#9,#10,#11,#13,#14,#15,#16,#17,#18,#19,#20,#21,#22,#23,#24,#25,#26,#27,#28,#29,#30,#31,#32,#33,#34,#35,#36,#37,#38,#39,#40,#41,#42,#43,#44,#45,#46,#47,#48,#49,#50').each(function(i){
			id = $(this).attr('id');
			thisval = $(this).val();
			if(thisval==0){
				$(this).val('0.0000');
			}
			lr = $(this).parent('td').next('td[class="lr"]');
			bl = lr.next('td[class="bl"]');
			
			if(id<12){
				if(id==7){
					thisval = thisval-(NnSum('#5,#8,#9,#10'));
					bl.text(Number(getNan(thisval/val_49)).toFixed(4)+'%');
					lr.text(Number(getNan(thisval)).toFixed(4)+'万元');
				}else if(id==11){
					thisval = Nn(thisval);
					bl.text(Number(getNan(thisval/Nn(val_49))).toFixed(4)+'%');
					lr.text(Number(getNan(val_49)).toFixed(4)+'万元');
				}else{
					thisval = Nn(thisval);
					bl.text(Number(getNan(thisval/Nn(val_49))).toFixed(4)+'%');
					lr.text(Number(getNan(thisval)).toFixed(4)+'万元');
				}
			}else{
				thisval = Nn(thisval);
				bl.text(Number(getNan(thisval/Nn(val_48))).toFixed(4)+'%');
				lr.text(Number(getNan(thisval/Nn(val_7))).toFixed(4)+'%');
			}
		});
	}
	function getNan(obj){
		obj = (isNaN(obj)||obj==Infinity?'0.0000':obj);
		return obj;
	}
	
	//控制 利润
	var aa = null;
	function checkLrBl(){
		if(aa!=null)clearTimeout(aa);
		aa = setTimeout(function(){
			GetBl();
		}, 1500);
	}
	GetBl();
});



function Nn(obj){
	if(obj==null||$.trim(obj)==''||obj==0.0||isNaN(obj)){
		return 0.0;
	}else{
		return Number(obj);
	}
}
function NnSum(obj){
	if(obj!=null){
		var sum = 0.0000;
		$(obj).each(function(){
			sum +=Nn($(this).val());
		});
		return sum;
	}
	return 0.0000;
}

function nullBooleanNumber(obj){
	if(obj==null||$.trim(obj)==''||obj==0.0000||obj==0||isNaN(obj)){
		return true;
	}else{
		return false;
	}
}